package com.netty.esc.dao.repository;

import com.netty.esc.entity.neo4j.node.PersonEntity;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;


/**
 * @author: ext.zhaoyang17
 * @date: 2022.11.15、
 * 或者叫PersonRepository也可..
 */
@Repository
public interface UserRepository extends Neo4jRepository<PersonEntity, Long> {

    //-----------------------------删增------------------------------
    /**
     * 创建新节点
     */
    @Query("MERGE (a:Person {id: $id}) RETURN a")
    Object createNode(@Param("id")Long id);


    /**
     * todo 删除节点待补充
     * 如果用户注销账户，则需要删除节点
     * */
    //@Query

    /**
     * 创建好友关系
     * */
//    @Query("MATCH (a:Person),(b:Person) WHERE a.id = {friend} AND b.id = {me} CREATE (a)-[w: friend ]->(b) return w")
    @Query("MATCH (a:Person)WITH a MATCH(b:Person) WHERE a.id = {me} AND b.id = {friend} MERGE (a)-[w: friend ]->(b) return w")
    Optional<Object> createFriendRelationShip(@Param("friend")Long friend, @Param("me")Long me, @Param("relationShip")String relationShip);

    /**
     * 解除好友关系
     * @param friend
     * @param me
     */
    @Query("match (a:Person)-[q:friend]-(w:Person) where a.id= {me} and w.id = {friend} delete q")
    void deleteByRepositoryFriend(@Param("friend")Long friend, @Param("me")Long me);


    /**
     * 解除单向拉黑关系
     * @param friend
     * @param me
     */
    @Query("match (a:Person)-[q:blacklist]-(w:Person) where a.id= {me} and w.id = {friend} delete q")
    void deleteByRepositoryBlackList(@Param("friend")Long friend, @Param("me")Long me);


    /**
     * 拉黑
     */
    @Query("MATCH (a:Person),(b:Person) WHERE a.id = {me} AND b.id = {target} MERGE (a)-[w: blacklist]->(b) return w")
    Optional<Object> createBlacklistRelationShip(@Param("target")Long target, @Param("me")Long me);


    //-----------------------------查询------------------------------
    /**
     * 根据用户id查询好友关系
     */
//    @Query("match (a:Person{id:$id})-[q:friend]-(w:Person) return w")
    @Query("match (a:Person)-[q:friend]-(w:Person) where a.id= $id and not (a)-[:blacklist]->(w) return w")
    Optional<List<PersonEntity>> findFriendRelation(@Param("id")Long id);

    /**
     * 根据双方id查询是否存在单向关系
     */
    @Query("MATCH (a:Person) -[q] -> (b:Person) where a.id = {start} and b.id = {end}  return q")
    Optional<List<Object>> find2User1DirectionRelationShip(@Param("start")Long start,@Param("end")Long end);

    /**
     * 根据双方id查询是否存在无向关系
     */
    @Query("MATCH (a:Person) -[q] - (b:Person) where a.id = {start} and b.id = {end}  return q")
    Optional<List<Object>> find2User1DirectionRelationShipByUndirected(@Param("start")Long start,@Param("end")Long end);





}
